package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.leaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private FeeValueMapper feeValueMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
//      判断是增加还是修改
        boolean b = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (b){
//          1.根据id删除公寓图片
            LambdaUpdateWrapper<GraphInfo> gWrapper = new LambdaUpdateWrapper<>();
            gWrapper.eq(GraphInfo::getId, apartmentSubmitVo.getId());
            gWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(gWrapper);
//          2.根据id删除公寓杂费值
            LambdaUpdateWrapper<ApartmentFeeValue> lWrapper = new LambdaUpdateWrapper<>();
            lWrapper.eq(ApartmentFeeValue::getId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(lWrapper);
//          3.根据id删除公寓标签
            LambdaUpdateWrapper<ApartmentLabel> labWrapper = new LambdaUpdateWrapper<>();
            labWrapper.eq(ApartmentLabel::getId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(labWrapper);
//          4.根据id删除公寓配套
            LambdaUpdateWrapper<ApartmentFacility> aWrapper = new LambdaUpdateWrapper<>();
            aWrapper.eq(ApartmentFacility::getId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(aWrapper);
//           5.批量增加公寓图片
              List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
              if(!CollectionUtils.isEmpty(graphVoList)) {
                  ArrayList<GraphInfo> graphInFoList = new ArrayList<>();
                  for (GraphVo graphVo : graphVoList) {
                      GraphInfo info = new GraphInfo();
                      info.setName(graphVo.getName());
                      info.setUrl(graphVo.getUrl());
                      info.setItemType(ItemType.APARTMENT);
                      info.setItemId(apartmentSubmitVo.getId());
                      graphInFoList.add(info);
                  }
                  graphInfoService.saveBatch(graphInFoList);
              }
//            6.批量增加公寓杂费值
              List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
              if(!CollectionUtils.isEmpty(feeValueIds)) {
                  ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
                  for (Long feeValueId : feeValueIds) {
                      ApartmentFeeValue value = new ApartmentFeeValue();
                      value.setFeeValueId(feeValueId);
                      value.setApartmentId(apartmentSubmitVo.getId());
                      apartmentFeeValues.add(value);
                  }
                  apartmentFeeValueService.saveBatch(apartmentFeeValues);
              }
//            7.批量增加公寓标签
              List<Long> labelIds = apartmentSubmitVo.getLabelIds();
              if(!CollectionUtils.isEmpty(labelIds)) {
                  ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
                  for (Long labelId : labelIds) {
                      ApartmentLabel apartmentLabel = new ApartmentLabel();
                      apartmentLabel.setLabelId(labelId);
                      apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                      apartmentLabels.add(apartmentLabel);
                  }
                  apartmentLabelService.saveBatch(apartmentLabels);
              }
//            8.批量增加公寓配套
              List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
              if (!CollectionUtils.isEmpty(facilityInfoIdList)){
                  ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
                  for (Long facilityId : facilityInfoIdList) {
                      ApartmentFacility apartmentFacility = new ApartmentFacility();
                      apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                      apartmentFacility.setFacilityId(facilityId);
                      facilityList.add(apartmentFacility);
                  }
                  apartmentFacilityService.saveBatch(facilityList);
              }
        }
    }

    @Override
    public IPage<ApartmentItemVo> querypageItem(Page<Object> page, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> result = apartmentInfoMapper.querypageItem(page,queryVo);
        return result;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
//      1.根据id查询公寓信息 这里的id是apartment_id(公寓id)
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
//      2.根据id查询图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,id);
//      3.根据id查询标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListLableInfoById(id);
//      4.根据id查询配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListfacilityInfoById(id);
//      5.根据id查询杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListfeeValueById(id);
//      6.组装结果
        ApartmentDetailVo adminApartmentDetailVo = new ApartmentDetailVo();

        BeanUtils.copyProperties(apartmentInfo, adminApartmentDetailVo);
        adminApartmentDetailVo.setGraphVoList(graphVoList);
        adminApartmentDetailVo.setLabelInfoList(labelInfoList);
        adminApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        adminApartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return adminApartmentDetailVo;
    }

    @Override
    public void removeALLById(Long id) {
//      查询占用房间数量
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0){
            throw new leaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }
//      删除公寓信息
        super.removeById(id);
//          1.根据id删除公寓图片
        LambdaUpdateWrapper<GraphInfo> gWrapper = new LambdaUpdateWrapper<>();
        gWrapper.eq(GraphInfo::getId,id);
        gWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(gWrapper);
//          2.根据id删除公寓杂费值
        LambdaUpdateWrapper<ApartmentFeeValue> lWrapper = new LambdaUpdateWrapper<>();
        lWrapper.eq(ApartmentFeeValue::getId,id);
        apartmentFeeValueService.remove(lWrapper);
//          3.根据id删除公寓标签
        LambdaUpdateWrapper<ApartmentLabel> labWrapper = new LambdaUpdateWrapper<>();
        labWrapper.eq(ApartmentLabel::getId,id);
        apartmentLabelService.remove(labWrapper);
//          4.根据id删除公寓配套
        LambdaUpdateWrapper<ApartmentFacility> aWrapper = new LambdaUpdateWrapper<>();
        aWrapper.eq(ApartmentFacility::getId,id);
        apartmentFacilityService.remove(aWrapper);
    }
}




